﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VMS.FTS.APPPUSH.Utils.FontData
{
    public static class ExtensionMethods
    {
        //public static int Compare(this string str, string str2)
        //{
        //    if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(str2))
        //        return 0;

        //    str = str.ToLower();
        //    str2 = str2.ToLower();
        //    int result = 100;
        //    int point = LevenshteinDistance.Compute(str, str2);
        //    result = (str.Length - point)*100/str.Length;
        //    return result;
        //}

        //public static bool Match(this string s1, string s2, int minPercentage)
        //{
        //    return s1.Compare(s2) > minPercentage;
        //}

        public static string base64Encode(this string data)
        {
            try
            {
                byte[] encData_byte = new byte[System.Text.Encoding.UTF8.GetBytes(data).Length];
                encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
                string encodedData = Convert.ToBase64String(encData_byte);
                return encodedData;
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string base64Decode(this string data)
        {
            try
            {
                System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
                System.Text.Decoder utf8Decode = encoder.GetDecoder();

                byte[] todecode_byte = Convert.FromBase64String(data);
                int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char = new char[charCount];
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                string result = new String(decoded_char);
                return result;
            }
            catch
            {
                return string.Empty;
            }
        }

        public static double GetValue(this double? value)
        {
            return !value.HasValue ? 0 : value.Value;
        }

        public static double GetDoubleValue(this string value)
        {
            double rtn = 0;
            double.TryParse(value, out rtn);

            return rtn;
        }

        public static byte GetByteValue(this string value)
        {
            byte rtn = 0;
            byte.TryParse(value, out rtn);

            return rtn;
        }

        public static int GetIntValue(this string value)
        {
            int rtn = 0;
            int.TryParse(value, out rtn);

            return rtn;
        }

        //public static double GetDistance(this DbGeometry basePoint, DbGeometry point)
        //{
        //    if (basePoint == null || basePoint.XCoordinate == null || point.XCoordinate == null)
        //        return 0;

        //    return GoogleDistanceAlgorithm.DistanceBetweenPlaces(basePoint.XCoordinate.GetValue(), basePoint.YCoordinate.GetValue(), 
        //                            point.XCoordinate.GetValue(), point.YCoordinate.GetValue());
        //}
    }
}
